package com.youkeda.app.service.impl;

import com.youkeda.app.dao.VotesDao;
import com.youkeda.app.dataobject.Votes;
import com.youkeda.app.model.Result;
import com.youkeda.app.service.TicketPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * @ClassName TicketPurchaseServiceImpl
 * @Author 刘正星
 * @Date 2020/8/25 7:47
 **/
@Service
public class TicketPurchaseServiceImpl implements TicketPurchaseService {
   /* @Autowired
    private VotesDao votesDao;
    @Autowired
    private RedisTemplate redisTemplate;



    @Override
    public Result<Boolean> ticketPurchase(long id) {


        //初始化返回数据
        Result result = new Result();
        result.setData(false);
        result.setSuccess(false);
        //实现购买代码
        //先去redis查询一下
       // redisTemplate.opsForValue().set(id,votesDao.selectByPrimaryKey(id).getStock());
        Object value = redisTemplate.opsForValue().get(id);
        int stock = 0;
        //如果redis没有则去数据库查询
        if (value == null){
            Votes votes = votesDao.selectByPrimaryKey(id);
            stock = votes.getStock();
            //将信息缓存到redis里边
            redisTemplate.opsForValue().set(votes.getId(),votes.getStock());
        }else {
            stock = (int)value;
            System.out.println("redis 缓存的库存"+stock);
        }
        //redis 事务解决并发问题
        redisTemplate.execute(new SessionCallback<List<Object>>() {
            @Override
            public List<Object> execute(RedisOperations redisOperations) throws DataAccessException {
               Integer stock = (Integer)redisOperations.opsForValue().get(id);
                //判断该商品的库存是否大于1
               if (Integer.valueOf(stock) >= 1){
                   //监听商品的名字，redis里边的key
                   redisOperations.watch(id);
                   //开启事务
                   redisOperations.multi();
                   //将该商品的库存自减1
                   redisOperations.opsForValue().set(id,stock-1);
                   //修改mysql数据库库存数量
                   Votes votes = new Votes();
                   votes.setId(id);
                   votes.setStock(stock-1);
                   votesDao.updateByPrimaryKeySelective(votes);
                   //执行事务
                   List exec = redisOperations.exec();
                   if (exec.size()>0){
                       //会执行成功的逻辑比如插入一条订单
                       System.out.println(exec);
                       System.out.println("抢购成功");
                       result.setData(true);
                   }else {
                       System.out.println(exec);
                       System.out.println("抢购失败");
                       result.setData(false);

                   }
                   return exec;
               }else {
                   System.out.println("商品数量不足！");
                   result.setSuccess(false);
                   result.setData(false);
                   return null;
               }

            }
        });
        return result;
    }*/

    @Autowired
    private VotesDao votesDAO;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Result<Boolean> ticketPurchase(long id) {

        //初始化返回数据
        Result result = new Result();
        result.setData(true);
        result.setSuccess(true);
        //实现购买代码
        //先去redis查询一下
        Object value = redisTemplate.opsForValue().get(id);
        int stock = 0;
        //如果redis没有则去数据库查询
        if (value==null) {
            //去数据库查询该商品的信息
            Votes votesDO = votesDAO.selectByPrimaryKey(id);
            //将信息缓存到redis里边
            redisTemplate.opsForValue().set(votesDO.getId(), votesDO.getStock());
            stock = votesDO.getStock();
        }else {
            stock=(int) value;
        }

        redisTemplate.execute(new SessionCallback<List<Object>>() {
            @Override
            public List<Object> execute(RedisOperations operations) throws DataAccessException {
                Integer stock = (Integer)operations.opsForValue().get(id);
                //判断该商品的库存是否大于1
                if (Integer.valueOf(stock) >= 1) {
                    //监听商品的名字，redis里边的key
                    operations.watch(id);

                    //开启事务
                    operations.multi();

                    //将该商品的库存自减1
                    operations.opsForValue().set(id, stock - 1);
                    //修改mysql数据库库存数量
                    Votes votesDO = new Votes();
                    votesDO.setId(id);
                    votesDO.setStock(stock - 1);
                    votesDAO.updateByPrimaryKeySelective(votesDO);
                    // 执行事务
                    List exec = operations.exec();
                    if (exec.size() > 0) {
                        //会执行成功的逻辑比如插入一条订单
                        System.out.println(exec);
                        System.out.println("抢票成功成功");
                        result.setData(true);
                    } else {
                        System.out.println(exec);
                        System.out.println("抢票失败失败");
                        result.setData(false);
                    }

                    return exec;
                } else {
                    System.out.println("无票");
                    result.setData(false);
                    return null;
                }
            }
        });

        return result;
    }
}
